/*=====================================================================================================================
                    Copyright(c) 2009 Geometric PLM Software Corp. All rights reserved.
                             Unpublished - All rights reserved
=======================================================================================================================
File description:
    Filename: tc_oci.h
    Module  : (geometric) itk module.

        This file manages the operations of OCI(Oracle Call Interface).

        OCI is the method of connecting the oracle supply by oracle with c/c++/java
        
        You need to include $ORACLE_HOME\OCI\include and $ORACLE_HOME\OCI\lib into your project configuration.
    Example: 
        HOW TO OPERATE THE DB?
        ---------------------------------------------------------------------------
        Method 1:
            Use db name, need *.ora file
        Parameters:
            sid=<server id>, user=<username>, pw=<password>
        Code:
            OCIData_t dbConn;
            int ociFail = GOCI_creat_evn_handle( &option->dbConn );
            if ( ociFail == OCI_SUCCESS )
            {
                // GOCI_logon( &option->dbConn, "infodba", "infodba", "TC2007" );
                ociFail = GOCI_logon( &option->dbConn, user, pw, sid );
            }
        ---------------------------------------------------------------------------
        Mehtod 2:
            user db link.
        Parameters:
            sid=<server ora string>, user=<username>, pw=<password>
            servername like:
            "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%1)(PORT=%2)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=%3)))"
        Code:
            OCIData_t dbConn;
            int ociFail = GOCI_creat_evn_handle( &option->dbConn );
            if ( ociFail == OCI_SUCCESS )
            {
                // GOCI_logon( &option->dbConn, "infodba", "infodba", "TC2007" );
                ociFail = GOCI_logon( &option->dbConn, user, pw, sid );
            }
            
=======================================================================================================================
Date               Name              Description of Change
29-Jul-2009        Penn,Xie          Initialize creation
$HISTORY$
=====================================================================================================================*/
#ifndef GITK_TC_OCI_H
#define GITK_TC_OCI_H
#include <oci.h>

// Define OCI handle struct
typedef struct OCIData_s
{ 
    OCIEnv    *env;
    OCIError  *error;
    OCISvcCtx *svc;
    OCIStmt   *sql;
    OCIDefine *define;
    OCIBind   *bind;
} OCIData_t; 

#ifdef __cplusplus
    extern "C"
    {
#endif

    /**
    * Creates env and initialize OCI handle.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_creat_evn_handle( OCIData_t *data );

    /**
    * Commit the execute.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_commit( OCIData_t *data );

    /**
    * Rollback all operations.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_rollback( OCIData_t *data );

    /**
    * Logon oracle database.
    * @param data - <IO> the oci object
    * @param user - <I> the user
    * @param pwd  - <I> the password
    * @param servername - <I> the server name
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_logon( OCIData_t *data, const char *user, const char *pwd, const char *servername );

    /**
    * Logon oracle database with ip
    * @param data - <IO> the oci object
    * @param ip - <I> the user
    * @param port - <I> the user
    * @param user - <I> the user
    * @param pwd  - <I> the password
    * @param servername - <I> the server name
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_logon_ip( OCIData_t *data, const char *ip, int port, const char *user, const char *pwd, const char *servername );

    /**
    * Prepares sql.
    * @param data - <IO> the oci object
    * @param sql  - <I> the sql
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_stmt_prepare( OCIData_t *data, const char *sql );

    /**
    * Binds data on array from database.
    * @param data - <IO> the oci object
    * @param pos  - <I> the sql
    * @param pos  - <I> the sql
    * @return - OCI_SUCCESS or error code
    */
    int GOCI_define_by_pos( OCIData_t *data, int pos, char *out, ub2 type );

    /**
    * Executes sql.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code.
    */
    sword GOCI_stmt_execute( OCIData_t *data );

    /**
    * Fetches data until output finish.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code.
    */
    int GOCI_stmt_fetch( OCIData_t *data );

    /**
    * Logoff and releases handles.
    * @param data - <IO> the oci object
    * @return - OCI_SUCCESS or error code.
    */
    void GOCI_handle_free( OCIData_t *data );

#ifdef __cplusplus
    }
#endif


#endif //GITK_TC_OCI_H
